use anyhow::Result;
use bytes::{BufMut, BytesMut};

fn main() -> Result<()> {
    let mut buf = BytesMut::with_capacity(1024);
    buf.put(&b"hello world"[..]);
    buf.put_u16(1234);

    let a = buf.split();
    assert_eq!(a, b"hello world\x04\xD2"[..]);

    buf.put(&b"goodbye world"[..]);

    let mut b = buf.split();
    assert_eq!(b, b"goodbye world"[..]);

    assert_eq!(buf.capacity(), 998);

    let mut buf = BytesMut::with_capacity(1024);
    buf.extend_from_slice(&b"hello world"[..]);
    buf.put_u16(1234);
    println!("buf: {buf:?}");

    let t = b.split();
    println!("{t:?}");

    //t freeze  t 不能访问  所有权是一个
    let m = t.freeze(); //inner data can not be change
    println!("{m:?}");

    let b = buf.split_to(12);
    println!("buf: {b:?}");

    Ok(())
}
